//
//  BannerCell.swift
//  旅航ios
//
//  Created by tulip on 2021/4/22.
//

import UIKit
import SwiftyJSON
import Kingfisher

class BannerCell: UITableViewCell,UICollectionViewDelegate,UICollectionViewDataSource {
    var data:[JSON]=[]
    var pageControl:WEIPageControl!
    @IBOutlet weak var searchBox: UIView!
    @IBOutlet weak var collect: UICollectionView!
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        
        let layout=UICollectionViewFlowLayout.init()
        layout.itemSize=CGSize(width: SCREEN_WIDTH, height: 209)
        layout.scrollDirection = .horizontal
        layout.sectionInset=UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        layout.minimumLineSpacing=0
        
        collect.backgroundColor=UIColor.clear
        collect.collectionViewLayout=layout
        collect.delegate=self
        collect.dataSource=self
        collect.isPagingEnabled=true
      //  collect.contentOffset=CGPoint(x: SCREEN_WIDTH, y: 0)
        collect.showsHorizontalScrollIndicator=false
        collect.register(UINib(nibName: "BannerCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "BannerCollectionViewCell")
       
        //page control
        pageControl = WEIPageControl()//初始化PageControl
        pageControl.frame = CGRect.init(x: 0, y:160, width: SCREEN_WIDTH, height: 10)
        pageControl.numberOfPages = 3//总页数
        pageControl.currentWidthMultiple = 4//当前点的宽度为其他点的4倍
        pageControl.pointSize = CGSize.init(width: 9, height: 9)//点的size
        pageControl.clickPoint { (index) in//圆点的点击事件
            self.collect.setContentOffset(CGPoint.init(x: SCREEN_WIDTH * CGFloat(index!), y: 0), animated: true)
        }
        self.addSubview(pageControl)
        //searchbox
        searchBox.shadow(type: .all, color: UIColor(red: 173.0 / 255.0, green: 173.0 / 255.0, blue: 173.0 / 255.0, alpha: 1), opactiy: 1, shadowSize: 3)
        
        
        Network.request(.home) { (JSON) in
            self.data=JSON["data"]["banner"].array ?? []
            self.collect.reloadData()
        } failure: { (err) in
            
        }

        

    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        self.selectionStyle = .none
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        data.count
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell:BannerCollectionViewCell=collectionView.dequeueReusableCell(withReuseIdentifier: "BannerCollectionViewCell", for: indexPath) as! BannerCollectionViewCell
        let url=data[indexPath.row]["img"].string ?? ""
        cell.img.kf.setImage(with: URL(string: url))
        return cell
    }
    
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {

        let index = scrollView.contentOffset.x / scrollView.frame.size.width
        let showIndex = lroundf(Float(index))
      
        pageControl.currentPage = showIndex

    }
    
    @IBAction func ariplane(_ sender: Any) {
        let vc=AirplaneVC.init()
        let cvc=getCurrentVc()
        cvc.navigationController?.pushViewController(vc, animated: true)
    }
    @IBAction func hotel(_ sender: Any) {
        let vc = HotelHomeController()
        let cvc = getCurrentVc()
        cvc.navigationController?.pushViewController(vc, animated: true)
    }
    @IBAction func travel(_ sender: Any) {
        let vc = TourismHomeController()
        let cvc = getCurrentVc()
        cvc.navigationController?.pushViewController(vc, animated: true)
    }
    @IBAction func train(_ sender: Any) {
        let vc = TrainTicketHomeController()
        let cvc = getCurrentVc()
        cvc.navigationController?.pushViewController(vc, animated: true)
    }
    
    @IBAction func oneClick(_ sender: Any) {
        
        let vc = WebLoadingViewController()
        
        
        let str = "http://swoole.whichzhen.com/api/user/app_h5?token=\(UserManager.shared.currentUser?.token ?? "")&uid=\(UserManager.shared.currentUser?.id ?? "")&type=3&city=成都"
        
        print("\(str)")
        
        vc.filePath = "http://swoole.whichzhen.com/api/user/app_h5?token=\(UserManager.shared.currentUser?.token ?? "")&uid=\(UserManager.shared.currentUser?.id ?? "")&type=3&city=成都"
        let cvc = getCurrentVc()
        cvc.navigationController?.pushViewController(vc,animated: true)
        
    }
    
    
    @IBAction func twoClick(_ sender: Any) {
        
        let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "OtherMainViewController") as! OtherMainViewController
        vc.flag = 0
        let cvc = getCurrentVc()
        cvc.navigationController?.pushViewController(vc,animated: true)
    }
    
    @IBAction func threeClick(_ sender: Any) {
        
        let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "OtherMainViewController") as! OtherMainViewController
        vc.flag = 1
        let cvc = getCurrentVc()
        cvc.navigationController?.pushViewController(vc,animated: true)
        
    }
    
    @IBAction func fourClick(_ sender: Any) {
        
        let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "OtherMainViewController") as! OtherMainViewController
        vc.flag = 2
        let cvc = getCurrentVc()
        cvc.navigationController?.pushViewController(vc,animated: true)
        
    }
}
